<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <!-- 已登录 -->
        <template v-if="token">
          <li>
            <a href="javascript:;"
              ><i class="iconfont icon-user"></i>{{ account || '周杰伦' }}</a
            >
          </li>
          <li><a href="javascript:;" @click="handleLogout">退出登录</a></li>
        </template>
        <!-- 未登录 -->
        <template v-else>
          <li><RouterLink to="/login">请先登录</RouterLink></li>
          <li><RouterLink to="/login">免费注册</RouterLink></li>
        </template>
        <li><a href="javascript:;">我的订单</a></li>
        <li><a href="javascript:;">会员中心</a></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li>
          <a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a>
        </li>
      </ul>
    </div>
  </nav>
</template>

<script>
import { vuexGetters } from '@/hooks/index'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import xtxMessage from '@/components/library/Message'

export default {
  name: 'AppTopnav',
  setup () {
    const store = useStore()
    const router = useRouter()

    const handleLogout = () => {
      try {
        store.dispatch('userStore/logout')
        router.push('/login')
        xtxMessage({ text: '用户退出' })
      } catch (e) {
        xtxMessage({ text: e, type: 'error' })
      }
    }

    return { ...vuexGetters(), handleLogout }
  }
}
</script>

<style scoped lang="less">
.app-topnav {
  background: #333;
  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;
    li {
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @xtxColor;
        }
      }
      ~ li {
        a {
          border-left: 2px solid #666;
        }
      }
    }
  }
}
</style>
